package com.example.myapplication2;

import java.util.ArrayList;
import java.util.*;
import java.util.UUID;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.MessageDigest;

class structWrapper {
    byte[] data;
    int pos;

    int leftLen() {
        return data.length - pos;
    }

    structWrapper(int l) {
        data = new byte[l];
        pos = 0;
    }

    structWrapper(byte[] data2) {
        data = data2;
        pos = 0;
    }

    public void writeByte(byte b) {
        data[pos] = b;
        pos += 1;
    }

    public void writeWord(short s) {
        byte[] t = ByteBuffer.allocate(2).order(ByteOrder.nativeOrder()).putShort(s).array();
        System.arraycopy(t, 0, data, pos, 2);
        pos += 2;
    }

    public void writeDWord(int s) {
        byte[] t = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(s).array();
        System.arraycopy(t, 0, data, pos, 4);
        pos += 4;
    }

    public void writeArr(byte[] s) {
        System.arraycopy(s, 0, data, pos, s.length);
        pos += s.length;
    }

    public byte[] readArr(int n) {
        byte[] re = new byte[n];

        System.arraycopy(data, pos, re, 0, n);
        pos += n;
        return re;
    }

    public void writeDouble(double d) {
        byte[] t = ByteBuffer.allocate(8).order(ByteOrder.nativeOrder()).putDouble(d).array();
        System.arraycopy(t, 0, data, pos, 8);
        pos += 8;
    }

    public byte[] getWriteData() {
        byte[] d = new byte[pos];
        System.arraycopy(data, 0, d, 0, pos);
        return d;
    }

    public byte readByte() {
        pos += 1;
        return data[pos - 1];

    }

    public short readWord() {
        pos += 2;
        return ByteBuffer.wrap(data, pos - 2, 2).order(ByteOrder.nativeOrder()).getShort();
    }

    public int readDWord() {
        pos += 4;
        return ByteBuffer.wrap(data, pos - 4, 4).order(ByteOrder.nativeOrder()).getInt();
    }

    public double readDouble() {
        pos += 8;
        return ByteBuffer.wrap(data, pos - 8, 8).order(ByteOrder.nativeOrder()).getDouble();
    }

    public byte[] getLeftData() {
        byte[] d = new byte[data.length - pos];
        System.arraycopy(data, pos, d, 0, data.length - pos);
        return d;
    }
}
